package com.lzq.rabbitmqdemo.amqp.constant;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    /**
     * 工作模式
     *
     */
    @Bean("workQueue")
    public Queue workQueue() {
        return QueueBuilder.durable(Constants.WORK_QUEUE).build();
    }


    /**
     *发布/订阅模式
     *
     */
    @Bean("fanoutQueue1")
    public Queue fanoutQueue1() {
        return QueueBuilder.durable(Constants.FANOUT_QUEUE1).build();
    }
    @Bean("fanoutQueue2")
    public Queue fanoutQueue2() {
        return QueueBuilder.durable(Constants.FANOUT_QUEUE2).build();
    }

    //声明交换机
    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange() {
        return ExchangeBuilder.fanoutExchange(Constants.FANOUT_EXCHANGE_NAME).durable(true).build();
    }

    //队列和交换机绑定
    @Bean
    public Binding fanoutBinding(@Qualifier("fanoutExchange") FanoutExchange
                                         exchange, @Qualifier("fanoutQueue1") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange);
    }
    @Bean
    public Binding fanoutBinding2(@Qualifier("fanoutExchange") FanoutExchange
                                          exchange, @Qualifier("fanoutQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange);
    }


    /**
     * 路由模式
     *
     */
    @Bean("directQueue1")
    public Queue routingQueue1() {
        return QueueBuilder.durable(Constants.DIRECT_QUEUE1).build();
    }
    @Bean("directQueue2")
    public Queue routingQueue2() {
        return QueueBuilder.durable(Constants.DIRECT_QUEUE2).build();
    }
    //声明交换机
    @Bean("directExchange")
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange(Constants.DIRECT_EXCHANGE_NAME).durable(true).build();
    }

    //队列2绑定orange
    @Bean
    public Binding directBinding(@Qualifier("directExchange") DirectExchange
                                         exchange, @Qualifier("directQueue1") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("orange");
    }
    //队列2绑定black, green
    @Bean
    public Binding directBinding2(@Qualifier("directExchange") DirectExchange
                                          exchange, @Qualifier("directQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("black");
    }
    @Bean
    public Binding directBinding3(@Qualifier("directExchange") DirectExchange
                                          exchange, @Qualifier("directQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("green");
    }

    /**
     * 通配符模式
     *
     */
    @Bean("topicsQueue1")
    public Queue topicsQueue1() {
        return QueueBuilder.durable(Constants.TOPICS_QUEUE1).build();
    }
    @Bean("topicsQueue2")
    public Queue topicsQueue2() {
        return QueueBuilder.durable(Constants.TOPICS_QUEUE2).build();
    }
    //声明交换机
    @Bean("topicExchange")
    public TopicExchange topicExchange() {
        return ExchangeBuilder.topicExchange(Constants.TOPICS_EXCHANGE_NAME).durable(true).build();
    }
    //队列和交换机绑定
    //队列1绑定 error, 仅接收error信息
    //队列2绑定 info, error: error,info息都接收
    @Bean
    public Binding topicBinding(@Qualifier("topicExchange") TopicExchange
                                        exchange, @Qualifier("topicsQueue1") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("*.error");
    }
    @Bean
    public Binding topicBinding2(@Qualifier("topicExchange") TopicExchange
                                         exchange, @Qualifier("topicsQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("#.info");
    }
    @Bean
    public Binding topicBinding3(@Qualifier("topicExchange") TopicExchange
                                         exchange, @Qualifier("topicsQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("*.error");
    }
}
